%**************************************************************%
%
% Scripts to calculate bubble velocity velocities with correlation
%
%**************************************************************

function [velocity_corr_bubble] = bubble_corr(void1_window,void2_window,distance_between_sensors,freq_aq,frame_diference_xcorr)
      
    %Calculates the velocity with Correlation
    size_void2_window = length(void2_window);   
    max_correlation_value_bubble = 0;                            %calculates the correlation between wiremeshes
    R = corrcoef(void1_window, void2_window);
    correlation_result_bubble = R(1,2);
    void1_window_corr = void1_window;
    index = 0; 
    % will not process the correlation more than 2x the general difference
    % between sensor 1 and sensor 2
    max_index_corr = 2*frame_diference_xcorr;     
    mean_void1_window_corr = mean(void1_window); %Calculates the average of void1 in the window
    mean_void2_window_shifted_aux = mean(void2_window); %Calculates the average of void2 in the window
    % Calculates the correlation between sensor 1 and sensor 2 data in the
    % interval maximum twice the difference from correlation, however does
    % not allow the sensor 2 signal destroy all the data from sensor 1 and
    % does not calculates all the window (case of small windows)
    frame_diference_corr_bubble=0;
    velocity_corr_bubble=0;
    while (index < max_index_corr) && (mean_void1_window_corr > (0.2*mean_void2_window_shifted_aux)) && (index < size_void2_window - 5)
    
        index = index + 1; 
        frame_diference_corr_bubble = index;
        void2_window_shifted_aux = circshift(void2_window,[0 -index]);    %shifts the data from wire-mesh 2
        void2_window_shifted_aux(size_void2_window - index:size_void2_window) = 0; %erases the last elements
        void1_window_corr(1,size_void2_window-index+1) = 0;               %erases the last element

       % does not allow the sensor 2 signal destroy all the data from sensor 1 and
       % does not calculates all the window (case of small windows)
        mean_void1_window_corr = mean(void1_window_corr);
        mean_void2_window_shifted_aux = mean(void2_window_shifted_aux);
        % Calculates the  correlation
        R = corrcoef(void1_window_corr, void2_window_shifted_aux);         %calculates the correlation between wiremeshes
        correlation_result_bubble = R(1,2);
%         corr_result_window_array(index) = correlation_result_bubble; %Can plot the corr with time
%         time_axis_corr_bubble(index) = index/freq_aq;
        if correlation_result_bubble >= max_correlation_value_bubble
            frame_diference_corr_bubble = index;        
            max_correlation_value_bubble = correlation_result_bubble;            
            delaytime_corr_bubble = frame_diference_corr_bubble/freq_aq;
            velocity_corr_bubble = (distance_between_sensors/delaytime_corr_bubble); %calculates the mean velocity       
        end        
    end

    % Fills the correlation plot to analyze visially if the correlation is not going up
    % again
    for index = (frame_diference_corr_bubble + 1) : (2*frame_diference_corr_bubble)
        void2_window_shifted_aux2 = circshift(void2_window,[0 -index]);    %shifts the data from wire-mesh 2
        void2_window_shifted_aux2(1,size_void2_window) = 0;            %erases the last element
        R = corrcoef(void1_window, void2_window_shifted_aux2);         %calculates the correlation between wiremeshes
        correlation_result_bubble = R(1,2);        
    end
    
return      
end